package com.lovo.addressbook;

import java.util.List;

public class BusinessObject {
    int currentPos;
    User[] users;
    PropertiesUtil proutil;
    private String fileName = "config/telephone.dat";
    
    public BusinessObject(){
        currentPos = 0;
        proutil = new PropertiesUtil(fileName);
        users = getAllUsers();
	}
	
	public User[] getAllUsers() {
		List <User> list = proutil.getUsers();
		User[] users2 = new User[list.size()];
		for(int i =0;i<list.size();i++){
			users2[i]=list.get(i);
		}
		return users2;
	}

	public User first() {
		currentPos =0;
		return users[currentPos];
	}


	public User previous() {
		if(currentPos >0){
			currentPos --;
		}
		return users[currentPos];
	}


	public User next() {
		if(currentPos < users.length - 1){
			currentPos ++;
		}
		return users[currentPos];
	}


	public User last() {
		currentPos = users.length - 1;
		return users[currentPos];
	}


	public void saveUser(User user) {
		proutil.save(user);
		users = getAllUsers();
		currentPos = Integer.parseInt(user.getNo()) - 1;
	}
    
    
}
